<template>
  <div class="app-container">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="商品id" width="180" prop="goodId"></el-table-column>
      <el-table-column label="商品名称" width="180" prop="goodName"></el-table-column>
      <el-table-column label="商品介绍  " width="180" prop="goodInfo"></el-table-column>
      <el-table-column label="商品图片" width="300">
        <template slot-scope="scope">
          <img :src="scope.row.goodImgUrl" width="300" height="200" />
        </template>
      </el-table-column>
      <el-table-column label="商品销量" width="180" prop="goodSell"></el-table-column>

      <el-table-column label="商品库存" width="180" prop="goodCount"></el-table-column>
      <el-table-column label="商品状态" width="180">
        <template slot-scope="scope">{{getStatus(scope.row.goodStatus)}}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑商品</el-button>
          </div>
          <div>
            <el-button size="mini" @click="handleSecKill(scope.$index, scope.row)">开启特惠</el-button>
          </div>
          <div>
            <el-button size="mini" @click="handleDelete(scope.row.goodId)">删除商品</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageParam.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageParam.pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog title="编辑商品" :visible.sync="editVisible" width="30%">
      <el-form
        :model="editform"
        :rules="rules"
        ref="editform"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="editform.goodName"></el-input>
        </el-form-item>
        <el-form-item label="商品分类" prop="goodType">
          <el-cascader
            v-model="editform.goodType"
            :options="options"
            :props="{ expandTrigger: 'hover' }"
            ref="cascader"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="商品信息" prop="goodInfo">
          <el-input v-model="editform.goodInfo"></el-input>
        </el-form-item>
        <el-form-item label="商品销量" prop="goodSell">
          <el-input v-model="editform.goodSell"></el-input>
        </el-form-item>
        <el-form-item label="商品库存" prop="goodCount">
          <el-input v-model="editform.goodCount"></el-input>
        </el-form-item>
        <el-form-item label="商品原价" prop="goodPrice">
          <el-input v-model="editform.goodPrice"></el-input>
        </el-form-item>
        <el-form-item label="商品现价" prop="goodNowPrice">
          <el-input v-model="editform.goodNowPrice"></el-input>
        </el-form-item>
        <el-form-item label="商品状态" prop="goodStatus">
          <el-radio-group v-model="editform.goodStatus">
            <el-radio :label="1">上架</el-radio>
            <el-radio :label="0">下架</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('editform')">编辑商品</el-button>
          <el-button @click="resetForm('editform')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="开启特惠" :visible.sync="secKillVisible" width="30%">
      <el-form
        :model="secKillform"
        :rules="rules"
        ref="seckillform"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="秒杀价格" prop="goodSeckillPrice">
          <el-input v-model="secKillform.goodSeckillPrice"></el-input>
        </el-form-item>
        <el-form-item label="秒杀库存" prop="stock">
          <el-input v-model="secKillform.stock"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="secKillform.startTime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="secKillform.endTime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="openSeckill('seckillform')">开启秒杀</el-button>
          <el-button @click="resetForm('editform')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPageGood, getgoodCount, updateGood, deleteGood } from "@/api/good";
import { getAllType } from "@/api/goodtype";
import { addSecKill } from "@/api/seckill";
export default {
  data() {
    return {
      tableData: [],

      total: 0,

      // 编辑
      editVisible: false,

      //秒杀
      secKillVisible: false,
      rules: {
        goodName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        goodInfo: [
          { required: true, message: "商品信息不能为空", trigger: "blur" },
        ],
        goodSell: [
          { required: true, message: "商品销量不能为空", trigger: "blur" },
        ],
        goodCount: [
          { required: true, message: "商品库存不能为空", trigger: "blur" },
        ],
        goodPrice: [
          { required: true, message: "商品价格不能为空", trigger: "blur" },
        ],
        goodNowPrice: [
          { required: true, message: "商品现价不能为空", trigger: "blur" },
        ],
        goodStatus: [
          { required: true, message: "商品状态不能为空", trigger: "blur" },
        ],
        goodSeckillPrice: [
          { required: true, message: "商品秒杀价不能为空", trigger: "blur" },
        ],
        stock: [
          { required: true, message: "商品秒杀库存不能空", trigger: "blur" },
        ],
        startTime: [
          {
            required: true,
            message: "商品秒杀开启时间不能为空",
            trigger: "blur",
          },
        ],
        endTime: [
          {
            required: true,
            message: "商品秒杀结束时间不能为空",
            trigger: "blur",
          },
        ],
      },
      // 编辑表单
      editform: {
        goodId: "",
        goodName: "",
        goodType: "",
        goodInfo: "",
        goodCount: "",
        goodSell: "",
        goodPrice: "",
        goodNowPrice: "",
        goodStatus: "",
      },

      //秒杀表单
      secKillform: {
        goodId: "",
        goodSeckillPrice: "",
        stock: "",
        startTime: "",
        endTime: "",
      },

      pageParam: {
        currentPage: 1,
        pageSize: 5,
      },

      //商品分类所有选项
      options: [],
    };
  },

  methods: {
    init() {
      console.log("初始化了");
      getPageGood(this.pageParam).then((res) => {
        this.tableData = res.data;
      });
      getgoodCount().then((res) => {
        this.total = res.data;
      });

      // 加载所有标签
      getAllType().then((res) => {
        var data = res.data;
        // 遍历父节点进行删除children  in 遍历拿数组索引 of 遍历 拿数组对象

        for (var item of data) {
          console.log(item);
          this.removeChildren(item);
        }
        this.options = data;

        this.options = res.data;
      });
    },

    //去除树型结构的children树型 参数为父节点
    removeChildren(node) {
      // 假设没有长度 或children 长度为0 删除该属性
      if (!node.children || !node.children.length) {
        delete node.children;
        // 要记得返回不然报错
        return;
      }

      // 遍历children 继续删除
      for (var item of node.children) {
        this.removeChildren(item);
      }

      return node;
    },

    handleSizeChange(val) {
      this.pageParam.pageSize = val;
      getPageGood(this.pageParam).then((res) => {
        this.tableData = res.data;
      });
    },

    handleCurrentChange(val) {
      this.pageParam.currentPage = val;
      getPageGood(this.pageParam).then((res) => {
        this.tableData = res.data;
      });
    },

    getStatus(status) {
      switch (status) {
        case 0:
          return "下架";
          break;
        case 1:
          return "上架中";
          break;
        case 2:
          return "特惠中";
          break;
      }
    },

    handleEdit(index, row) {
      this.editVisible = true;
      this.editform = row;
    },

    handleSecKill(index, row) {
      this.secKillVisible = true;
      this.secKillform = {};
      this.secKillform.goodId = row.goodId;
    },

    handleDelete(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteGood(id).then((res) => {
            if (res.code == 200) {
              this.init();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 设置参数
          var goodType = this.$refs.cascader.getCheckedNodes(true);
          console.log(goodType);
          this.editform.goodType = goodType[0].value;
          updateGood(this.editform).then((res) => {
            if (res.code == 200) {
              this.init();
              this.editVisible = false;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 开启秒杀
    openSeckill(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          // 设置参数
          console.log("开始时间", this.secKillform.startTime);
          console.log("结束时间", this.secKillform.endTime);

          // this.secKillform.startTime = new Date(this.secKillform.startTime);
          // this.secKillform.endTime = new Date(this.secKillform.endTime);

          //提交秒杀请求
          addSecKill(this.secKillform).then((res) => {
            if (res.code == 200) {
              this.init();
              this.secKillform = "";
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },

  created() {
    this.init();
  },
};
</script>

<style>
</style>